import { createContext } from 'react';
import { observable, action, computed } from 'mobx';
import { $service, $storage, $qiniu } from '@/cross/utils';
import { ISnsProfile } from '../../types';

class store {
  // state
  @observable snsProfile: ISnsProfile = {};

  @action getMyProfile = (clearCache?: boolean) => {
    return new Promise((resolve, reject) => {
      if (this.snsProfile.user_id && !clearCache) return resolve(this.snsProfile);
      $service('sns_profile', 'getMyProfile')
        .then((profile) => {
          console.log('[profile] ', profile);
          this.snsProfile = profile;
          resolve(profile);
        })
        .catch((e) => {
          console.log('[e] ', e);
          reject(e);
        });
    });
  };
}

const storeIns = new store();

export const storeContext = createContext(storeIns);

export default storeIns;
